<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%
String paId_almacen = request.getParameter("id_almacen");
String Id_Almacen = (String) session.getAttribute("id_almac") == null ? (String) session.getAttribute("id_almac") : (String) session.getAttribute("id_almac");
//out.println("<br> ------"+Id_Almacen);
String parAlmOr = request.getParameter("AlmOr");
if (parAlmOr!= null && parAlmOr.equals("ao")) {
	if (paId_almacen!=null && !paId_almacen.equals("")) {
		Id_Almacen = paId_almacen;
	} else {
 	Id_Almacen = "0";
	}
} else {
	if (Id_Almacen==null) {
	 	Id_Almacen = "0";
	}
}


String parCampo1 = request.getParameter("campo1");
String parCampo2 = request.getParameter("campo2");
String parCampo3 = request.getParameter("campo3");
String parCampo4 = request.getParameter("campo4");
String parCampo5 = request.getParameter("campo5");

String rsId_ruta = "";
String rsCodigo = "";
String rsAlmacen_Or = "";
String rsAlmacen_OrCod = "";
String rsAlmacen_OrNom = "";
String rsAlmacen_Des = "";
String rsAlmacen_DesCod = "";
String rsAlmacen_DesNom = "";

String parCampo = request.getParameter("campo");
String parOrden = request.getParameter("orden");
parCampo = (parCampo!=null && !parCampo.equals("codigo"))?"descripc":"codigo";
parOrden = (parOrden!=null && !parOrden.equals("asc"))?"desc":"asc";
String varImgC = "";
String varImgD = "";

//out.print(parCampo+" "+parOrden);

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query = "SELECT A.id_ruta, A.codigo AS rCod, B.codigo AS aoCod, B.nombre AS aoNomb, C.codigo AS adCod , C.nombre AS adNomb "+
				"FROM ruta AS A, almacen AS B, almacen AS C "+
				"WHERE A.almacen_origen = B.id_almacen and A.almacen_destino = C.id_almacen "+
				"AND A.almacen_origen = "+Id_Almacen+" AND A.almacen_origen IS NOT NULL "+
				"ORDER BY ";

	if (parCampo.equals("codigo")) {
		if (parOrden.equals("desc")) {
			query += "adCod DESC";
		} else {
			query += "adCod ASC";
		}
	} else {
		if (parOrden.equals("desc")) {
			query += "adNomb DESC";
		} else {
			query += "adNomb ASC";
		}
	}
	
//out.print(query);
PreparedStatement StatementRs = ConnRs.prepareStatement(query);
ResultSet Rsquery = StatementRs.executeQuery();
boolean Rsquery_hasData = Rsquery.next();
Object Rsquery_data;

%>
<html>
<head>
<title>CIMA v2 - Cat&aacute;logo Rutas</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="miestilo.css" type="text/css">
<script>
//---------
	function enviaOrden(opc) {
		document.form1.campo.value = opc;
		document.form1.orden.value = document.form1.orden.value == "asc"?"desc":"asc";
		document.form1.submit();
	}
//---------
	function enviarSel() {
		obj1 = eval(<%="window.opener.form1."+parCampo1%>);
		//obj2 = eval(<%="window.opener.form1."+parCampo2%>);
		obj3 = eval(<%="window.opener.form1."+parCampo3%>);
		//obj4 = eval(<%="window.opener.form1."+parCampo4%>);
		obj5 = eval(<%="window.opener.form1."+parCampo5%>);
		
		//if (obj1 && obj2 && obj3 && obj4 && obj5) {
		if (obj1 && obj3 && obj5) {
			inicio = 0;
			fin = 0;
			texto1 = document.form1.selCodigo.value;
			texto2 = document.form1.selCodigo.options[document.form1.selCodigo.options.selectedIndex].text;
			//alert(texto1);
			fin = texto1.indexOf("|",0);
			obj1.value = texto1.substring(inicio,fin);
			//alert(obj1.value);
			inicio = fin+1;
			fin = texto1.indexOf("|",inicio);
			//obj2.value = texto1.substring(inicio,fin);
			//alert(obj2.value);
			inicio = fin+1;
			obj3.value = texto1.substring(inicio,texto1.length);
			//alert(obj3.value);
			inicio = 0;
			fin = 0;
			fin = texto2.indexOf("|",0);
			inicio = fin+2;
			fin = texto2.indexOf("|",inicio);
			//obj4.value = texto2.substring(inicio,fin);
			//alert(obj4.value);
			//inicio = fin+2;
			obj5.value = texto2.substring(inicio,texto2.length);
			//alert(obj5.value);


			window.close();
		}
	}
//---------
  function escribe(selobj,textobj)
   {
     id = selobj.options.selectedIndex;
     textobj.value = selobj.options[id].text;
   }

//----------
  function marca(selobj,textobj)
   {
	 texto = textobj.value;
     tam_texto = textobj.value.length;
     contador = selobj.options.length;
     for (i=0;i<=contador-1;i++)
       {
         resultado = selobj.options[i].text.substr(0,tam_texto);
         if (resultado == texto)
            {
               selobj.options[i].selected = true;
               break;
            }
       }
   }
//----------
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="images/fondo.jpg">
<br>
<form name="form1" method="post" action="rutas_catalogo.jsp">
  <table border="0" align="center" cellpadding="0" cellspacing="0">
  <%
	//--------------------------------------------------------------------
	String NombreAlmacen = "";
	if (Id_Almacen!=null && !Id_Almacen.equals("")) {
		  	Connection ConnRsAlmacen = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
			String queryAlmacen = "";
				    queryAlmacen += "SELECT nombre ";
					queryAlmacen += "FROM almacen ";
					queryAlmacen += "WHERE id_almacen = "+Id_Almacen+" AND id_almacen IS NOT NULL ";
					queryAlmacen += "AND id_almacen > 0";
			//out.print(queryAlmacen);
			PreparedStatement StatementRsAlmacen = ConnRsAlmacen.prepareStatement(queryAlmacen);
			ResultSet RsqueryAlmacen = StatementRsAlmacen.executeQuery();
			if (RsqueryAlmacen!=null && RsqueryAlmacen.next()) {
				NombreAlmacen = RsqueryAlmacen.getString("nombre")!=null?RsqueryAlmacen.getString("nombre"):"&nbsp;";
			}
			else { NombreAlmacen = ""; }
			ConnRsAlmacen.close();
		}		
		%>
    <tr> 
      <td class="titulos" align="center">'<%=NombreAlmacen%>' - Ruta</td>
  </tr>
  <tr> 
      <td align="center" bgcolor="#D6E0F5"> C&oacute;digo &nbsp; 
        <input type="text" name="buscCodigo" class="textfields" onKeyUp="marca(document.form1.selCodigo,document.form1.buscCodigo)" size="25">
      </td>
  </tr>
  <tr> 
    <td>&nbsp;
        <input type="hidden" name="orden" value="<%=parOrden%>"><input type="hidden" name="campo" value="<%=parCampo%>">
		<input type="hidden" name="campo1" value="<%=parCampo1%>"><input type="hidden" name="campo2" value="<%=parCampo2%>">
		<input type="hidden" name="campo3" value="<%=parCampo3%>">
      </td>
  </tr>
  <tr> 
    <td>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
		  <% 
		  if (parCampo.equals("codigo") && parOrden.equals("asc")) {
		  		varImgC = "<img src=\"images/fechaA.gif\">&nbsp;";
			} else  if (parCampo.equals("codigo") && parOrden.equals("desc")) {
					varImgC = "<img src=\"images/fechaB.gif\">&nbsp;";
				} else {
					varImgC = "";
				}

		  if (parCampo.equals("descripc") && parOrden.equals("asc")) {
		  		varImgD = "<img src=\"images/fechaA.gif\">&nbsp;";
			} else  if (parCampo.equals("descripc") && parOrden.equals("desc")) {
					varImgD = "<img src=\"images/fechaB.gif\">&nbsp;";
				} else {
					varImgD = "";
				}


		  %>
            <td align="center" class="titcolumna" width="26%" height="20"><%=varImgC%><a href="javascript:enviaOrden('codigo')">Codigo</a></td>
            <td align="center" class="titcolumna"><%=varImgD%><a href="javascript:enviaOrden('descrip')">Alm. Dest.</a></td>
          </tr>
        </table>
      </td>
  </tr>
  <tr> 
      <td align="center"> 
        <select name="selCodigo" class="textfields" size="12" style=" width:250px;" onChange="escribe(document.form1.selCodigo,document.form1.buscCodigo)">
          <% while (Rsquery_hasData) { 
				rsId_ruta = Rsquery.getString("id_ruta");
				rsCodigo = Rsquery.getString("rCod");
				rsAlmacen_OrCod = Rsquery.getString("aoCod");
				rsAlmacen_OrNom = Rsquery.getString("aoNomb");
				rsAlmacen_DesCod = Rsquery.getString("adCod");
				rsAlmacen_DesNom = Rsquery.getString("adNomb");
		%>
          <option value="<%=rsId_ruta+"|"+rsAlmacen_OrCod+"|"+rsAlmacen_DesCod%>"><%=rsAlmacen_DesCod+" | "+rsAlmacen_DesNom%></option>
          <% Rsquery_hasData = Rsquery.next();
		} %>
        </select>
      </td>
  </tr>
</table>
<br>
  <table align="center" cellpadding="0" cellspacing="0">
    <tr> 
    <td> 
        <a href="javascript:enviarSel()"><img src="images/bAceptar.gif" width="82" height="18" border="0"></a>&nbsp; 
        <a href="javascript:window.close()"><img src="images/bCerrar.gif" width="82" height="18" border="0"></a></td>
  </tr>
 </table>
</form>
</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
<%
Rsquery.close();
StatementRs.close();
ConnRs.close();
%>